package com.yaozheng.vocationaltraining.fragment.html5;

import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.fragment.BaseFragment;
import com.yaozheng.vocationaltraining.iview.collect.IMyCollectAddView;
import com.yaozheng.vocationaltraining.service.collect.MyCollectAddService;
import com.yaozheng.vocationaltraining.service.impl.collect.MyCollectAddServiceImpl;
import com.yaozheng.vocationaltraining.utils.LogUtils;
import com.yaozheng.vocationaltraining.utils.NumberUtils;
import com.yaozheng.vocationaltraining.utils.StringUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * html5页面
 */
@EFragment(R.layout.fragment_html5)
public class Html5Fragment extends BaseFragment implements IMyCollectAddView {
    @ViewById
    TextView headTitleText;
    @ViewById
    WebView html5WebView;
    @ViewById
    ImageView headRightImage;

    String newId;

    String titile;

    String contentUrl;

    @Bean(MyCollectAddServiceImpl.class)
    MyCollectAddService myCollectAddService;

    public void setContentUrl(String contentUrl) {
        this.contentUrl = contentUrl;
    }

    public void setTitile(String titile) {
        this.titile = titile;
    }



    @AfterViews
    public void initView() {
        myCollectAddService.init(this);
        headTitleText.setText(titile);
        headRightImage.setVisibility(View.GONE);
        headRightImage.setImageResource(R.drawable.icon_favorites);
        getBaseActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
        //启用支持javascript
        WebSettings settings = html5WebView.getSettings();
        settings.setJavaScriptEnabled(true);
        // 设置可以支持缩放
        settings.setSupportZoom(true);
// 设置出现缩放工具
        settings.setBuiltInZoomControls(true);

        //扩大比例的缩放
        settings.setUseWideViewPort(true);
//自适应屏幕
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);

        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        html5WebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                LogUtils.println("shouldOverrideUrlLoading="+url);

                return false;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
//                LogUtils.println("onPageStarted=" + url);
                newId = StringUtils.getNewsId(url);
                if (newId != null) {
                    headRightImage.setVisibility(View.VISIBLE);
                } else {
                    headRightImage.setVisibility(View.GONE);
                }
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
//                LogUtils.println("onPageFinished=" + url);
                super.onPageFinished(view, url);
            }
        });


        html5WebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                headTitleText.setText(title);
            }

        });
        html5WebView.loadUrl(contentUrl);
    }

    @Override
    public boolean backPressed() {
        if (html5WebView.canGoBack()) {
            html5WebView.goBack();
//            LogUtils.println("backPressed=" + html5WebView.getUrl());
            return false;
        } else {
            return super.backPressed();
        }
    }

    public void rightClick() {
        showOperatingProgressDialog("收藏中...");
        if (newId != null) {
            myCollectAddService.addCollect(2, NumberUtils.stringToInt(newId));
        }
    }

    @UiThread
    @Override
    public void addCollectSuccess(JSONObject jsonObject) {
        cancelOperatingProgressDialog();
        boolean result = TypeUtils.getJsonBoolean(TypeUtils.getJsonObject(jsonObject, "data"), "result", false);
        if (result) {
            alertMessage("收藏成功!");
        } else {
            alertMessage("收藏失败!请与服务器管理员联系");
        }
    }

    @UiThread
    @Override
    public void addCollectError(String error) {
        cancelOperatingProgressDialog();
        alertMessage(error);
    }

    @Override
    public void onDestroyView() {
        newId = null;
        getBaseActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onDestroyView();
    }
}
